<?php

class Baiviet_model extends CI_Model
{
	// Protected or private properties
	protected $_table;
	
	// Constructor
	public function __construct()
	{
		parent::__construct();
		$this->_table = $this->config->item('database_tables');
		$this->load->helper('date');
		date_default_timezone_set('Asia/Ho_Chi_Minh');
	}

	// Public methods
	public function get_all_article($limit,$start)
	{
		$this->db->select('site_content.*, site_category.id as category_id, site_category.name');
		$this->db->from('site_content','site_category');
		$this->db->join('site_category','site_category.id = site_content.category_id');
		$this->db->order_by('site_content.id', 'DESC');
		$this->db->limit($limit,$start);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	public function count_all_baiviet()
	{
		$this->db->select('id');
		$this->db->from('site_content');
		return $this->db->count_all_results();
	}
	
	public function get_all_category()
	{
		$this->db->select('id,name');
		$this->db->from('site_category');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	public function add_article($image)	
	{
	
		$alias = auto_alias(strtolower($this->input->post('alias')));
		$create_date = ($this->input->post('publish_date') != '') ? date('Y-m-d', strtotime($this->input->post('publish_date'))) : date('Y-m-d');
		//$image = $this->upload->data();
		$data = array
					(
						'title' 		=> $this->input->post('title'),
						'alias' 		=> $alias,
						'description' 	=> $this->input->post('description'),
						'content'		=> $this->input->post('content'),
						'category_id'	=> $this->input->post('category'),
						'publish_date'	=> $create_date,
						'expired_date'	=> $create_date,
						'created_date'  => $datetime = date('Y-m-d H:i:s'),
						'featured'		=> $this->input->post('featured'),
						'creator_id'	=> $this->user->id,
						'status'		=> $this->input->post('status'),
						'content_image'		=>($image) ? $image['file_name']:'',
					
						//'thumb'			=> $image['file_name']
					);
		//echo 'a'; exit();
		$this->db->insert('site_content', $data);
	}
	public function add_baiviet()	
	{
	
		$alias = auto_alias(strtolower($this->input->post('alias')));
		$publish_date = ($this->input->post('publish_date') != '') ? date('Y-m-d', strtotime($this->input->post('publish_date'))) : date('Y-m-d');
		//$image = $this->upload->data();
		$data = array
					(
						'title' 		=> $this->input->post('title'),
						'alias' 		=> $alias,
						'description' 	=> $this->input->post('description'),
						'content'		=> $this->input->post('content'),
						'category_id'	=> $this->input->post('category'),
						'created_date'  => $datetime = date('Y-m-d H:i:s'),
						'publish_date'	=> $publish_date,
						'expired_date'	=> $publish_date,
						'featured'		=> $this->input->post('featured'),
						'creator_id'	=> $this->user->id,
						'status'		=> $this->input->post('status'),
					
						//'thumb'			=> $image['file_name']
					);
		//echo 'a'; exit();
		$this->db->insert('site_content', $data);
	}
	
	public function change_status_article($id = null,$status = 0)
	{
		$data = array
				(
					'status'=> $status
				);
		$this->db->where('id',$id);
		$this->db->update('site_content',$data);		
	}
	
	public function change_featured_article($id = null,$status = 0)
	{
		$data = array
				(
					'featured'=> $status
				);
		$this->db->where('id',$id);
		$this->db->update('site_content',$data);		
	}
	
	
	public function delete_article($id)
	{
		$this->db->where('id', $id);
		$this->db->delete('site_content');
	}
	
	public function get_article($id)
	{
		$this->db->select('site_content.*, site_category.id as categoryid, site_category.name');
		$this->db->from('site_content','site_category');
		$this->db->join('site_category','site_category.id = site_content.category_id');
		$this->db->where('site_content.id',$id);
		$query = $this->db->get();
		return	$result = $query->row();

		
	}
	
	public function edit_article($id, $image,$file)
	{
		if($this->input->post('alias')=='')
		{
			$alias = strtolower(auto_alias($this->input->post('title')));
		}
		else
		{
			$alias = auto_alias(strtolower($this->input->post('alias')));
		}
		$publish_date = ($this->input->post('publish_date') != '') ? date('Y-m-d', strtotime($this->input->post('publish_date'))) : date('Y-m-d');
		$data = array(
			'title' => $this->input->post('title'),
			'alias' => $alias,
			'description' 	=> $this->input->post('description'),
			'category_id'	=> $this->input->post('category'),
			'publish_date'	=> $publish_date,
			'expired_date'	=> $publish_date,
			'featured'		=> $this->input->post('featured'),
			'edited_by'		=> $this->user->id,
			'status' 		=> $this->input->post('status'),
			'content_image'		=>($image) ? $image['file_name']:'',
			//'small'			=>($image) ? $image['image_thumb']:'',
		);
		@unlink('uploads/content/'.$file);
		@unlink('uploads/content/medium/'.$file);
		@unlink('uploads/content/small/'.$file);
		$this->db->where('id', $id);
		$this->db->update('site_content', $data);
	}
	
	public function edit_article1($id)
	{
		//echo html_entity_decode($this->input->post('content'), ENT_NOQUOTES, 'UTF-8');
		if($this->input->post('alias')=='')
		{
			$alias = strtolower(auto_alias($this->input->post('title')));
		}
		else
		{
			$alias = auto_alias(strtolower($this->input->post('alias')));
		}
		$publish_date = ($this->input->post('publish_date') != '') ? date('Y-m-d', strtotime($this->input->post('publish_date'))) : date('Y-m-d');
		$data = array(
			'title' 		=> $this->input->post('title'),
			'alias' 		=> $alias,
			'description' 	=> $this->input->post('description'),
			'content' 		=> html_entity_decode($this->input->post('content'), ENT_NOQUOTES, 'UTF-8'),//$this->input->post('content', true),// html_entity_decode($this->input->post('content')),
			'category_id'	=> $this->input->post('category'),
			'publish_date'	=> $publish_date,
			'expired_date'	=> $publish_date,
			'featured'		=> $this->input->post('featured'),
			'edited_by'		=> $this->user->id,
			'status' 		=> $this->input->post('status'),
		);
		$this->db->where('id', $id);
		$this->db->update('site_content', $data);
	}
	
	public function check_alias()
	{
		$alias = auto_alias(strtolower($this->input->post('alias')));
        $query =  "(
			SELECT id, alias
			FROM site_content WHERE alias ='$alias' )
			UNION (			
			SELECT id, alias
			FROM site_category WHERE alias = '$alias')
			UNION(
			SELECT id, alias
			FROM site_page WHERE alias = '$alias')
			";
        $result = $this->db->query($query);
        if($result->num_rows() >= 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

/* End of file posts_model.php */
/* Location: ./application/modules/admin/models/posts_model.php */